package cn.sll.system.service.impl;

import cn.sll.system.domain.Role;
import cn.sll.system.mapper.MenuMapper;
import cn.sll.system.mapper.PermissionMapper;
import cn.sll.system.mapper.RoleMapper;
import cn.sll.system.service.IRoleService;
import cn.sll.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sll
 * @since 2023-01-03
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);

        permissionMapper.addRolePermission(role.getId(),role.getPermissions());
        menuMapper.addRoleMenu(role.getId(),role.getMenus());
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteRolePermission(id);
        menuMapper.deleteRoleMenu(id);
        super.delete(id);
    }

    @Override
    public void update(Role role) {
        permissionMapper.deleteRolePermission(role.getId());
        menuMapper.deleteRoleMenu(role.getId());

        super.update(role);
        permissionMapper.addRolePermission(role.getId(),role.getPermissions());
        menuMapper.addRoleMenu(role.getId(),role.getMenus());
    }

}
